package com.example.spring_ai.mcp.tools;

import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

@Slf4j
public class CustomTools {

    @Tool(description = "获取当前系统时间")
    public String getCurrentTime() {
        String time = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        log.info("getCurrentTime called: {}", time);
        return time;
    }

    @Tool(description = "执行数学运算（支持加减乘除）")
    public double calculateExpression(
            @Parameter(name = "expression", description = "数学表达式，如 3+5*2") String expr
    ) {
        log.info("calculateExpression called: {}", expr);
        Expression expression = new ExpressionBuilder(expr).build();
        try {
            return expression.evaluate();
        } catch (Exception e) {
            throw new RuntimeException("表达式解析失败: " + e.getMessage());
        }
    }
}
